package com.study.d012Components.listview;

import java.util.Comparator;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
*@description 使用包含部分可观察对象的属性对象来    测试可观察列表的动作
*@author xtayhame
*date 2020年3月13日
*version
*since v1.0
*/
public class ListViewEventWithObject2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		//ObservableList<Student2> datas=FXCollections.observableArrayList();

		//通过这种方式获取的列表，在进行set数据时，不需要进行refresh()方法进行刷新
		ObservableList<Student3> datas = FXCollections.observableArrayList(new Callback<Student3, Observable[]>() {
			@Override
			public Observable[] call(Student3 param) {
				Observable[]  osb = new Observable[]{param.getAge()};
				return osb;
			}
		});

		Student3 s1 = new Student3("张三",18);
		Student3 s2 = new Student3("李四",19);
		Student3 s3 = new Student3("王五",30);
		Student3 s4 = new Student3("赵柳",55);





		datas.addAll(s1,s2,s3,s4);

		Button b1  = new Button("点击");
		ListView<Student3> listView = new ListView<>(datas);
		listView.setPrefSize(200, 300);
		anchorPane.getChildren().addAll(b1,listView);
		AnchorPane.setLeftAnchor(listView, 50.0);
		AnchorPane.setTopAnchor(listView, 50.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();


		//




		//数据转换
		listView.setCellFactory(TextFieldListCell.forListView(new StringConverter<Student3>() {

			@Override
			public String toString(Student3 object) {

				return object.getName() + "-" + object.getAge().get();
			}

			@Override
			public Student3 fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		}));



		datas.addListener(new ListChangeListener<Student3>() {
			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends Student3> c) {
				if(c.next()){
					if(c.wasAdded()){
						System.out.println("新增动作");
					}
					if(c.wasPermutated()){
						System.out.println("排序动作");
					}
					if(c.wasRemoved()){
						System.out.println("移除动作");
					}
					if(c.wasReplaced()){
						System.out.println("替换动作");
					}
					if(c.wasUpdated()){
						System.out.println("更新动作");
					}
				}
			}
		});


		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				//datas.set(0, "abcdef");    //新增、移除、替换
				//datas.add("abcdef");   //新增
				//datas.remove(0);   // 移除


				//排序
				/*datas.sort(new Comparator<Student2>() {

					@Override
					public int compare(Student2 o1, Student2 o2) {
						// TODO Auto-generated method stub
						return o2.getName().get().compareTo(o1.getName().get());
					}
				});*/

				//这里主要代码顺序，因为age是可观察属性，所有setAge()写在后面，刷新时会同事刷新到setName()方法
				s1.setName("李逵");
				s1.setAge(90);
				//listView.refresh();
			}
		});
	}

}
